package com.yanqu.road.server.manager.union.huaben.rank;

import com.yanqu.road.entity.rank.cross.CrossUnionWeekRank;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.RankListHelper;
import com.yanqu.road.pb.union.cross.UnionHuaBenProto;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenRankMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.stream.IntStream;

public class CrossGroupWeekUnionRankListModel {

	private int rankNum = GameConfig.RANK_DEFAULT_MAX_COUNT;

	private CrossUnionWeekRankSort sorter;

	private LinkedList<CrossUnionWeekRank> rankList;

	private int groupId;

	private int weekId;

	private boolean isUpdate = false;

	public int getGroupId() {
		return groupId;
	}

	public int getWeekId() {
		return weekId;
	}

	public void setRankNum(int rankNum) {
		this.rankNum = rankNum;
	}

	public List<UnionHuaBenProto.UnionHuaBenUnionWeekRankTempMsg> getRankListMsg() {
		return CrossUnionHuaBenRankMgr.parseUnionRankListMsg(rankList);
	}

	public LinkedList<CrossUnionWeekRank> getRankList() {
		return rankList;
	}

	public CrossGroupWeekUnionRankListModel(int groupId, int weekId) {
		sorter = new CrossUnionWeekRankSort();
		rankList = new LinkedList<>();
		this.groupId = groupId;
		this.weekId = weekId;
	}

	public void initRankList() {
		rankList = new LinkedList<>();
	}

	public void setRankList(LinkedList<CrossUnionWeekRank> unionRankList) {
		synchronized (this) {
			rankList = new LinkedList<>(unionRankList);
			RankListHelper.checkUnionWeekRankListValid(rankList);       //校验数组
			rankList.sort(sorter);
			while (rankList.size() > rankNum){
				rankList.removeLast();
			}
		}
	}

	//领取排行奖励一定要用加锁的
	public int getMyRank(String unionUid) {
		synchronized (this) {
			return unlockGetMyRank(unionUid);
		}
	}

	//排行榜排名可以不加锁
	public int unlockGetMyRank(String unionUid) {
		OptionalInt result = IntStream.range(0, rankList.size())
				.filter(i -> Objects.equals(rankList.get(i).getUnionUid(), unionUid)).findFirst();
		if (result.isPresent()) {
			return result.getAsInt() + 1;
		}
		return -1;
	}

	public void removeRank(String unionUid) {
		if (StringUtils.isNullOrEmpty(unionUid)) {
			return;
		}
		synchronized (this) {
			rankList.removeIf(m -> Objects.equals(m.getUnionUid(), unionUid));
		}
	}

	public LinkedList<CrossUnionWeekRank> removeRank(Set<Long> serverIdSet) {
		LinkedList<CrossUnionWeekRank> result = new LinkedList<>();
		synchronized (this) {
			Iterator<CrossUnionWeekRank> iterator = rankList.iterator();
			while(iterator.hasNext()){
				CrossUnionWeekRank rank = iterator.next();
				if(!serverIdSet.contains(rank.getServerId())){
					if(rank.getValue().compareTo(BigInteger.ZERO) > 0) {
						result.add(rank.clone());
						rank.setValue(BigInteger.ZERO);
						iterator.remove();    //策划需求要优化
					}
				}
			}
			if(result.size() > 0) {
				rankList.sort(sorter);
				isUpdate = true;
			}
		}
		return result;
	}

	/**
	 * 添加到排行
	 */
	public boolean rankChange(CrossUnionWeekRank unionRank) {
		if(null == unionRank){
			return false;
		}
		boolean isChange = false;
		synchronized (this) {
			int size = rankList.size();
			if (!rankList.stream().anyMatch(predicate -> Objects.equals(predicate.getUnionUid(), unionRank.getUnionUid()))) {
				if (size < rankNum) {
					CrossUnionWeekRank unionRankTemp = unionRank.clone();
					unionRankTemp.setLastUpdateTime(System.currentTimeMillis() / 1000);
					rankList.add(unionRankTemp);
					isChange = true;
				} else {
					CrossUnionWeekRank rank = rankList.getLast();
					if (sorter.compare(unionRank, rankList.getLast()) < 0) {
						rank.setUnionUid(unionRank.getUnionUid());
						rank.setUnionBaseInfo(unionRank.getUnionBaseInfo());
						rank.setLastUpdateTime(System.currentTimeMillis() / 1000);
						rank.setServerId(unionRank.getServerId());
						rank.setValue(unionRank.getValue());
						rank.setParam(unionRank.getParam());
						isChange = true;
					}
				}
			} else {
				rankList.forEach(action -> {
					if (Objects.equals(action.getUnionUid(), unionRank.getUnionUid())) {
						action.setUnionBaseInfo(unionRank.getUnionBaseInfo());
						action.setLastUpdateTime(System.currentTimeMillis() / 1000);
						action.setValue(unionRank.getValue());
						action.setParam(unionRank.getParam());
					}
				});
				isChange = true;
			}
			rankList.sort(sorter);
		}
		if(isChange){
			isUpdate = true;
		}
		return isChange;
	}

}
